package com.xinsoft.config;

import com.xinsoft.jwt.JwtAuthenticationTokenFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * @author hsm
 * @description 安全配置
 * @createDate 2023-04-24
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Resource
    private UserDetailsService userDetailsService;

    @Resource
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable()
                .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
                .logout()
                .logoutUrl("/signout")
                .and()
                .authorizeRequests()
                .antMatchers("/login", "/refreshtoken").permitAll()
                .anyRequest().authenticated()
                .and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers("/role/add")
                .antMatchers("/role/getRoles/sales")
                .antMatchers("/role/getRoles")
                .antMatchers("/validateImages/**")
                .antMatchers("/business/mobileBusinessRegister")
                .antMatchers("/dir/productType")
                .antMatchers("/fileOperation")
                .antMatchers("/registerUser")
                .antMatchers("/login")
                .antMatchers("/loginByPDA")
                .antMatchers("/zte/**")
                .antMatchers("/system/weChart/**")
                .antMatchers("/system/company/**")
                .antMatchers("/swagger-ui/**")
                .antMatchers("/system/dict/**")
                .antMatchers("/system/dept/**")
                .antMatchers("/swagger-resources/**")
                .antMatchers("/images/**")
                .antMatchers("/websocket/**")
                .antMatchers("/druid/**")
                .antMatchers("/webjars/**")
                .antMatchers("/v3/api-docs/**")
                .antMatchers("/doc.html")
                .antMatchers("/configuration/ui")
                .antMatchers("/configuration/security")
                .antMatchers("/css/**", "/fonts/**", "/img/**", "/js/**", "/skins/**", "/favicon.ico");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
